﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BusinessEntity;
using System.IO;
using Foundation;
using Google.Documents;
using Google.GData.Documents;

namespace GDocsMobile
{
    public partial class FormMain : Form
    {
        private void CreateMessage(string lsMessage)
        {
            MessageBox.Show(lsMessage, "GDocs Mobile Client", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        private void CreateException(Exception ex)
        {
            MessageBox.Show(ex.Message, "GDocs Mobile Client", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public FormMain()
        {
            InitializeComponent();

            Session.mGDocsWrapper = new GDocsWrapper();
            Session.mGDocsWrapper.LoginRequiredEvent += new GDocsBase.LoginRequiredEventHandler(mGDocsWrapper_LoginRequiredEvent);

            // Init DocListType
            comboBoxDocListType.Items.Add(DocListTypeEnum.Local.ToString());
            comboBoxDocListType.Items.Add(DocListTypeEnum.Remote.ToString());
            comboBoxDocListType.SelectedItem = DocListTypeEnum.Local.ToString();

            RefreshDocList();
        }

        void mGDocsWrapper_LoginRequiredEvent()
        {
            Session.mGDocsWrapper.Login();
        }

        private void RefreshDocList()
        {
            if (comboBoxDocListType.SelectedItem.ToString() == DocListTypeEnum.Local.ToString())
            {
                dataGrid1.ContextMenu = contextMenuLocal;
                RefreshLocalDocList();
            }
            else if (comboBoxDocListType.SelectedItem.ToString() == DocListTypeEnum.Remote.ToString())
            {
                dataGrid1.ContextMenu = contextMenuRemote;
                RefreshRemoteDocList();
            }
        }

        private void RefreshLocalDocList()
        {
            try
            {
                Session.mGDocsWrapper.GetDocsList();
                dTFileBindingSource.Filter = "DocListType='" + DocListTypeEnum.Local.ToString() + "'";
                dTFileBindingSource.DataSource = Session.mGDocsWrapper.AllDSFile;
            }
            catch (Exception ex)
            {
                CreateException(ex);
            }

        }


        private void RefreshRemoteDocList()
        {
            try
            {
                dTFileBindingSource.Filter = "DocListType='" + DocListTypeEnum.Remote.ToString() + "'";
                dTFileBindingSource.DataSource = Session.mGDocsWrapper.AllDSFile;
            }
            catch (Exception ex)
            {
                CreateException(ex);
            }
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            FormOptions lFormOptions = new FormOptions();
            lFormOptions.ShowDialog();
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            FormAbout lFormAbout = new FormAbout();
            lFormAbout.ShowDialog();
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            FormNew lFormNew = new FormNew();
            lFormNew.ShowDialog();
        }

        private void FormMain_Activated(object sender, EventArgs e)
        {
            RefreshDocList();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (comboBoxDocListType.SelectedItem.ToString() == DocListTypeEnum.Remote.ToString())
            {
                Session.mGDocsWrapper.GetGDocsList();
            }
            RefreshDocList();
        }

        private void comboBoxDocListType_SelectedValueChanged(object sender, EventArgs e)
        {
            RefreshDocList();
        }

        private void menuOpen_Click(object sender, EventArgs e)
        {
            FileInfo lFileInfo = (FileInfo)((BusinessEntity.Data.DSFile.DTFileRow)((System.Data.DataRowView)dTFileBindingSource.Current).Row).ColFile;
            System.Diagnostics.Process.Start("pword.exe", "-opendoc " + lFileInfo.FullName + "");
        }

        private void menuDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to delete the selected file locally?", "GDoc Mobile Client", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                try
                {
                    FileInfo lFileInfo = (FileInfo)((BusinessEntity.Data.DSFile.DTFileRow)((System.Data.DataRowView)dTFileBindingSource.Current).Row).ColFile;
                    File.Delete(lFileInfo.FullName);
                }
                catch (Exception ex)
                {
                    CreateException(ex);
                }
            }
        }

        private void menuUpload_Click(object sender, EventArgs e)
        {
            try
            {
                // Do nada for now
                FileInfo lFileInfo = (FileInfo)((BusinessEntity.Data.DSFile.DTFileRow)((System.Data.DataRowView)dTFileBindingSource.Current).Row).ColFile;
                Session.mGDocsWrapper.Upload(lFileInfo);

                CreateMessage("Upload Completed");
            }
            catch (Exception ex)
            {
                CreateException(ex);
            }
        }

        private void menuItemDownload_Click(object sender, EventArgs e)
        {
            // Do nada for now
            try
            {
                Document lDocument = (Document)((BusinessEntity.Data.DSFile.DTFileRow)((System.Data.DataRowView)dTFileBindingSource.Current).Row).ColFile;
                Session.mGDocsWrapper.Download(lDocument);
                //CreateMessage("Download Completed");
            }
            catch (Exception ex)
            {
                CreateMessage(ex.Message);
            }
        }

        private void menuItemDeleteRemote_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to delete the selected files from remote site?\nThe Selected File will be moved to Trash remotely.", "GDoc Mobile Client", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                try
                {
                    Document lDocument = (Document)((BusinessEntity.Data.DSFile.DTFileRow)((System.Data.DataRowView)dTFileBindingSource.Current).Row).ColFile;
                    Session.mGDocsWrapper.Delete(lDocument);
                    ((System.Data.DataRowView)dTFileBindingSource.Current).Row.Delete();
                }
                catch (Exception ex)
                {
                    CreateMessage(ex.Message);
                }
            }
        }

        private void comboBoxDocListType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}